package com.ryan.ohos.extension.scroll;

import ohos.agp.components.Component;

public class ScrollableHelper {
    public static boolean canScrollHorizontally(Component component, int direction) {
        if (component instanceof Scrollable) {
            Scrollable scrollable = (Scrollable) component;
            return scrollable.canScrollHorizontally(direction);
        } else {
            return component.canScroll(Component.HORIZONTAL);
        }
    }

    public static boolean canScrollVertically(Component component, int direction) {
        if (component instanceof Scrollable) {
            Scrollable scrollable = (Scrollable) component;
            return scrollable.canScrollVertically(direction);
        } else {
            return component.canScroll(Component.VERTICAL);
        }
    }

    /**
     * canScrollHorizontally impl for {@link Scrollable#canScrollHorizontally(int)}
     *
     * @param scrollable The given view to check.
     * @param direction Negative to check scrolling left, positive to check scrolling right.
     * @return true if this view can be scrolled in the specified direction, false otherwise.
     */
    public static boolean canScrollHorizontallyImpl(Scrollable scrollable, int direction) {
        final int offset = scrollable.computeHorizontalScrollOffset();
        final int range = scrollable.computeHorizontalScrollRange();
        if (range == 0) return false;
        if (direction < 0) {
            return offset > 0;
        } else {
            return offset < range - 1;
        }
    }

    /**
     * canScrollVertically impl for {@link Scrollable#canScrollVertically(int)}
     *
     * @param scrollable The given view to check.
     * @param direction Negative to check scrolling up, positive to check scrolling down.
     * @return true if this view can be scrolled in the specified direction, false otherwise.
     */
    public static boolean canScrollVerticallyImpl(Scrollable scrollable, int direction) {
        final int offset = scrollable.computeVerticalScrollOffset();
        final int range = scrollable.computeVerticalScrollRange();
        if (range == 0) return false;
        if (direction < 0) {
            return offset > 0;
        } else {
            return offset < range - 1;
        }
    }
}
